import requests, time
from bs4 import BeautifulSoup

import pymysql
import random
import string
import hashlib
import os
from flask import Flask, render_template, request, url_for


def str2number(valuestr):
    valuestr = str(valuestr)
    idx_of_wan = valuestr.find('万')
    if idx_of_wan != -1:
        return int(float(valuestr[:idx_of_wan]) * 1e4)
    elif idx_of_wan == -1:
        return int(valuestr)


class food:
    def __init__(self, name_cai, shoucang_cai, href_cai, chengFen_cai, tuPian_cai):
        self.name_cai = name_cai

        self.shoucang_cai = shoucang_cai
        self.href_cai = href_cai
        self.chengFen_cai = chengFen_cai
        self.tuPian_cai = tuPian_cai


def get_db_connection():
    conn = pymysql.connect(host="localhost", port=3306, user='root', password='',
                           charset='utf8mb4', db='kafka')

    return conn





def generate_random_key(length):
    characters = string.ascii_letters + string.digits + string.punctuation
    key = ''.join(random.choice(characters) for _ in range(length))
    return key


random_key = generate_random_key(128)

salt = os.urandom(64)
password = random_key
salted_password = salt + password.encode()
hashed_password = hashlib.sha256(salted_password).hexdigest()
final_key = salt.hex() + ":?|<`" + hashed_password

app = Flask(__name__)
app.config['SECRET_KEY'] = final_key
app.start_time = time.time()


@app.route('/pictures', methods=['GET', 'POST'])
def pictures():
    return render_template('pictures.html')


@app.route('/', methods=('POST', 'GET'))
def denglu():
    return render_template("denglu.html")


@app.route('/yemian', methods=['GET', 'POST'])
def AnKe():
    return render_template('ZhuYe.html')


@app.route('/pa', methods=['GET', 'POST'])
def kkk():
    url_list = []

    caipu_list = ['川菜', '湘菜', '粤菜', '鲁菜']  # 中国菜系
    for caipu in caipu_list:
        for x in range(10):
            url = 'https://www.douguo.com/caipu/{}/0/{}'.format(caipu, x * 20)
            url_list.append(url)

    headers = {

        'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'

    }

    i = 0
    keshe_list = []
    for url in url_list:
        response = requests.get(url, headers=headers)

        # print(response.content.decode('utf-8'))
        soup = BeautifulSoup(response.text, 'html.parser')

        name = soup.find_all('a', class_='cookname text-lips')

        chengFen = soup.find_all('p', class_='major')

        tuPian = soup.find_all('a', class_='cook-img')

        name_cai = [i.string for i in name]
        for l in name_cai:
            print(l)

        href_cai = [('https://www.douguo.com' + i.attrs['href']) for i in name]
        shoucang = []
        for h in href_cai:
            print(h)
            shoucangResponse = requests.get(h, headers=headers)
            soup = BeautifulSoup(shoucangResponse.text, 'html.parser')
            shou = soup.find('span', class_='collectnum')
            print(shou)

            if shou:
                shoucang.append(str2number(shou.text))
            else:
                shoucang.append("N")
            time.sleep(random.uniform(2, 5))
        chengFen_cai = [i.string for i in chengFen]
        for o in chengFen_cai:
            print(o)
        tuPian_cai = [i.find('img').get('src') for i in tuPian]

        leng = len(name_cai)
        for u in range(leng):
            fo = food(name_cai[u], shoucang[u], href_cai[u], chengFen_cai[u], tuPian_cai[u])
            keshe_list.append(fo)
        time.sleep(random.uniform(4, 20))
        i += 1
        print("第", i, "个网页爬取成功")
    conn = get_db_connection()
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    create_table_sql = """
                CREATE TABLE keshe (

                    name_cai     VARCHAR(255) PRIMARY KEY,
                    shoucang_cai  VARCHAR(255),
                    href_cai     VARCHAR(255),
                    chengFen_cai VARCHAR(255),
                    tuPian_cai   VARCHAR(255)
                );
                """
    # 检查表是否存在
    check_table_sql = "SHOW TABLES LIKE 'keshe'"
    cursor.execute(check_table_sql)
    exists = cursor.fetchone()

    if not exists:
        # 执行建表语句
        try:
            cursor.execute(create_table_sql)
            conn.commit()
            print("表创建成功")
        except pymysql.Error as e:
            conn.rollback()
            print("创建表时出错：", e)

    for t in keshe_list:
        name_cai = t.name_cai
        shoucang_cai = t.shoucang_cai
        href_cai = t.href_cai
        chengFen_cai = t.chengFen_cai
        tuPian_cai = t.tuPian_cai

        cursor.execute(
            'INSERT IGNORE INTO  keshe (name_cai,shoucang_cai,href_cai,chengFen_cai,tuPian_cai) VALUES (%s, %s,%s, %s,%s)',
            [name_cai, shoucang_cai, href_cai, chengFen_cai, tuPian_cai])
        conn.commit()
    cursor.close()
    conn.close()
    return render_template('kkk.html')


@app.route('/index', methods=['GET', 'POST'])
def index():
    # 调用上面的函数，获取链接，这个index.html就是总导航菜单页面
    conn = get_db_connection()

    # 查询所有数据，放到变量posts中
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute('SELECT * FROM keshe ')
    posts = cursor.fetchall()

    cursor.close()
    conn.close()
    if posts == None:
        return "卧槽，爬太快又被锁ip了"
    PAGESIZE = 5

    total_posts = len(posts)
    print(total_posts)

    # 获取当前请求的页码，默认为第一页
    page = int(request.args.get('page', 1))

    # 计算总页数
    total_pages = (total_posts + PAGESIZE - 1) // PAGESIZE

    # 确定当前页开始和结束的文章索引
    start_index = (page - 1) * PAGESIZE
    end_index = min(start_index + PAGESIZE, total_posts)

    # 获取当前页文章列表
    current_page_posts = posts[start_index:end_index]
    # 获取服务器启动时间（单位：秒）
    startup_time = time.time() - app.start_time  # 假设app.start_time记录了服务器启动时间戳

    # 将运行时间转换为天、小时、分钟、秒
    days, remainder = divmod(startup_time, 86400)  # 一天有86400秒
    hours, remainder = divmod(remainder, 3600)
    minutes, seconds = divmod(remainder, 60)

    # 构造运行时间字符串
    uptime_str = f"{int(days)}天{int(hours)}小时{int(minutes)}分钟{seconds:.1f}秒"

    return render_template('index.html',
                           posts=current_page_posts,
                           current_page=page,
                           total_pages=total_pages,
                           uptime=uptime_str
                           )


if __name__ == '__main__':
    app.debug = True
    app.run(host="0.0.0.0", port=10007)
